package com.dao;

import com.pojo.Emp;
import com.pojo.Page;
import com.utils.DSUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class EmpDao {
    public ArrayList<Emp> queryEmp() {
        return DSUtil.query("select * from emp limit 0,5",Emp.class);
    }

    public ArrayList<Emp> queryEmp(int nowPage) {
        return DSUtil.query("select * from emp limit ?,?",Emp.class,(nowPage-1)*5,5);
    }

    public void delete(String id) {
        DSUtil.execute("delete from emp where id = ?", id);
    }

    public Emp queryById(String id) {
        return DSUtil.query("select * from emp where id = ?",
                Emp.class, id).get(0);
    }


    public void update(Emp emp) {
        DSUtil.execute("update emp set name = ?, age = ?, hobby = ?, sex = ?,  deptname = ? where id = ?",
                emp.getName(), emp.getAge(), emp.getHobby(), emp.isSex(), emp.getDeptname(), emp.getId());
    }

    public void insert(Emp emp) {
        Date date = emp.getHiredate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String sd = sdf.format(date);
        System.out.println(sd);
        DSUtil.execute("insert into emp(name,age,hobby,sex,deptname,hiredate) values(?,?,?,?,?,?)",
                emp.getName(), emp.getAge(), emp.getHobby(), emp.isSex(), emp.getDeptname(),sd);
    }

    public ArrayList<Emp> query(String search) {
        String s = "%" + search + "%";
        ArrayList<Emp> list = DSUtil.query("select * from emp where name like ? or age like ? or sex like ? or hobby like ? or deptname like ?",
                Emp.class, s, s, s, s, s);
        return list;
    }

    public int queryPages() {
        return DSUtil.query("select count(*) as page from emp", Page.class).get(0).getPage();

    }
}
